package org.example.springboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.example.springboot.entity.BorrowRecord;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Map;

/**
 * 借阅记录Mapper接口
 */
@Mapper
public interface BorrowRecordMapper extends BaseMapper<BorrowRecord> {
    
    /**
     * 获取最近12个月的借阅数据
     * @return 每月借阅数量
     */
    @Select("SELECT DATE_FORMAT(borrow_time, '%Y-%m') AS month, COUNT(*) AS count " +
            "FROM borrow_record " +
            "WHERE borrow_time >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH) " +
            "GROUP BY month " +
            "ORDER BY month ASC")
    List<Map<String, Object>> selectMonthlyBorrowData();
    
    /**
     * 获取最近12个月的归还数据
     * @return 每月归还数量
     */
    @Select("SELECT DATE_FORMAT(actual_return_time, '%Y-%m') AS month, COUNT(*) AS count " +
            "FROM borrow_record " +
            "WHERE actual_return_time IS NOT NULL " +
            "AND actual_return_time >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH) " +
            "GROUP BY month " +
            "ORDER BY month ASC")
    List<Map<String, Object>> selectMonthlyReturnData();
} 